<?php

namespace App\Store\Models\OldMaterial;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;

class OldData extends Model
{
    protected $table = 'site_oldmaterial_olddata';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     * 列表
     */
    public static function getList($request)
    {
        $cp_id = $request->cp_id ?? '';
        $query = self::orderBy('id', 'desc');
        $query->where('is_del',1);
        $query->where('cp_id',$cp_id);
        // 回收门店
        $store_name = $request->store_name ?? '';
        if ($store_name != '') {
            $query->where('store_name', 'like', '%' . $store_name . '%');
        }

        // 回收时间
        $start_time = $request->start_time;
        $end_time = $request->end_time;
        if ($start_time && $end_time) {
            $query->where('stockin_at', '>=', $start_time)->where('stockin_at', '<=', $end_time);
        }

        // 订单号
        $order_sncode = $request->order_sncode ?? '';
        if ($order_sncode != '') {
            $query->where('order_sncode', 'like', '%' . $order_sncode . '%');
        }

        // 条码
        $goods_sn_code = $request->goods_sn_code ?? '';
        if ($goods_sn_code != '') {
            $query->where('goods_sn_code', $goods_sn_code);
        }

        // 品牌
        $brand = $request->brand ?? '';
        if ($brand != '') {
            $query->where('brand', 'like', '%' . $brand . '%');
        }

        $isgetall = $request->isgetall ?? 0;

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }
}

